(function($) 
{ 
	"use strict";

  // for banner height js
  var windowWidth = $(window).width();
  var windowHeight =$(window).height();
  $('.banner.is-home').css({'height':windowHeight });

  function count(options) {
      var $this = $(this);
      options = $.extend({}, options || {}, $this.data('countToOptions') || {});
      $this.countTo(options);
  }

  $(document).ready(function()
  {
    
    var windowBottom = $(window).height()+0;
    var index=0;
    var divposition, divsrollpos, ctop;
    $(document).scroll(function(){
      if ($(".factabout").length > 0)
      {

          divposition = parseInt($('.factabout').offset().top);
          divsrollpos = parseInt($(window).scrollTop());
          ctop = parseInt(divposition-divsrollpos);
          if(ctop<Math.round(windowBottom/2)){
              if(index==0){

                  $('.timer').each(count);

              }
              index++;
          }
      }
    });

    // Bx slider -- slider on single page and blog page;
    if ($('.featured-post-slider').length > 0)
    {
      $('.featured-post-slider').bxSlider(
      {
        auto: false,
        mode: 'horizontal',
        pager: false,
        controls: true,
        nextText: '',
        prevText: ''
      })
    }

    // Google Map
    if ( $("#qva-googlemap").length>0 )
    { 
        var _self = $("#qva-googlemap"),
           $longitude = _self.data("longitude"),
           $latitude  = _self.data("latitude"),
           $title  = _self.data("title"),
           $image  = _self.data("image");



           if ( $longitude != '' && $latitude != '' )
           {
             
            var $latlng             = new  google.maps.LatLng($longitude,$latitude),
                $myOptions          = {
                    zoom            : 16,
                    center          : $latlng,
                    panControl      : false,
                    zoomControl     : true,
                    scaleControl    : false,
                    mapTypeControl  : false,
                    // mapTypeId       : google.maps.MapTypeId.ROADMAP
                },

                // $tabContact         = ('tab-contact');

            // $content.bind('easytabs:after', function(evt,tab,panel) {
                // if ( tab.hasClass($tabContact) ) {
            $map = new google.maps.Map(document.getElementById("qva-googlemap"), $myOptions);
            new google.maps.Marker({
                position: $latlng,
                map: $map, 
                title: $title,
                icon: $image
            });
         }
    }

    // our work
    if ($(".worklogo").length>0)
    {
      $('.worklogo').flexslider(
      {
        animation: "slide",
        slideshow: false,
        itemWidth: 210,
        itemMargin: 5,

      });
    }

    if ($("#banner").length > 0 && $("#banner").hasClass("is-slider") )
    {
      $('#banner').superslides({
          animation: 'fade',
          play: 5000
      });
    }

    // Somth page scroll
    $('a[href*=#]:not([href=#])').click(function() 
    {
      if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
        if (target.length) {
          $('html,body').animate({
            scrollTop: target.offset().top -63
          }, 1000);
          return false;
        }
      }
    });

    // for portfoli lightbox jquary
    var $chosenSheet,
    $stylesheets = $( "a[id^=theme-]" );
    
    // run rlightbox
    if ($(".lb").length >0)
    {
      $( ".lb" ).rlightbox();
    }
    if ($( ".lb_title-overwritten" ).length>0)
    {
      $( ".lb_title-overwritten" ).rlightbox({overwriteTitle: true}); 
    }

    if ($(".flexslider").length>0)
    {
      $('.flexslider').flexslider(
      {
        animation: "fade",
        animationLoop: true,
        slideshow: true,
        pauseOnAction: false,
        slideshowSpeed: 7000,
        controlNav: true,
        start: function(slider){
          $('body').removeClass('loading');
        }
      });
    }

    var $container = $('.portfolioContainer');
    if ($container.length>0)
    {
      $container.isotope({
          filter: '*',
          animationOptions: {
              duration: 750,
              easing: 'linear',
              queue: false
          }
      });
    }
    $('.portfolioFilter a').click(function(){
        $('.portfolioFilter .current').removeClass('current');
        $(this).addClass('current');
 
        var selector = $(this).attr('data-filter');
        $container.isotope({
            filter: selector,
            animationOptions: {
                duration: 750,
                easing: 'linear',
                queue: false
            }
         });
         return false;
    });

  //================== Video full div ======================
  var $allVideos = $("iframe[src^='http://player.vimeo.com'], iframe[src^='http://www.youtube.com'], object, embed"),
  $fluidEl = $("figure");
      
  $allVideos.each(function() 
  {
    $(this)
      // jQuery .data does not work on object/embed elements
      .attr('data-aspectRatio', this.height / this.width)
      .removeAttr('height')
      .removeAttr('width');
  });  

  $(window).resize(function() {

  var newWidth = $fluidEl.width();
  $allVideos.each(function() {
  
    var $el = $(this);
    $el
        .width(newWidth)
        .height(newWidth * $el.attr('data-aspectRatio'));
  
  });

  }).resize();


  // back home
  $(".logo").click(function()
  {
    window.location = woHomeUrl;
  })

  }) // end document ready

  $(window).load( function(){
      if ($(".portfolioFilter").length>0)
      {
          $(".portfolioFilter .current").trigger("click");
      }
  })


}(jQuery));
